package spring.cloud.tasks.api.controller;


import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import spring.cloud.tasks.api.service.AlarmStatisticsService;
import spring.cloud.tasks.api.utils.Response;

import javax.annotation.Resource;

/**
 * 所有集群的告警统计
 *
 * @author hebelala
 */
@RequestMapping("/console/alarmStatistics")
public class OverallAlarmStatisticsController {

    @Resource
    private AlarmStatisticsService alarmStatisticsService;

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/abnormalJobs")
    public Response getAbnormalJobs(@RequestParam(required = false) String zkClusterKey)
            throws RuntimeException {
        if (StringUtils.isBlank(zkClusterKey)) {
            return Response.success(alarmStatisticsService.getAbnormalJobsString());
        }
        return Response.success(alarmStatisticsService.getAbnormalJobsStringByZKCluster(zkClusterKey));
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/unableFailoverJobs")
    public Response getUnableFailoverJobs(@RequestParam(required = false) String zkClusterKey)
            throws RuntimeException {
        if (StringUtils.isBlank(zkClusterKey)) {
            return Response.success(alarmStatisticsService.getUnableFailoverJobsString());
        }
        return Response.success(alarmStatisticsService.getUnableFailoverJobsStringByZKCluster(zkClusterKey));
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/timeout4AlarmJobs")
    public Response getTimeout4AlarmJobs(@RequestParam(required = false) String zkClusterKey)
            throws RuntimeException {
        if (StringUtils.isBlank(zkClusterKey)) {
            return Response.success(alarmStatisticsService.getTimeout4AlarmJobsString());
        }
        return Response.success(alarmStatisticsService.getTimeout4AlarmJobsStringByZKCluster(zkClusterKey));
    }


    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/countOfAlarmJobs")
    public Response getCountOfAlarmJobs() throws RuntimeException {
        return Response.success(alarmStatisticsService.getCountOfAlarmJobs());
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/abnormalContainers")
    public Response getAbnormalContainers(@RequestParam(required = false) String zkClusterKey)
            throws RuntimeException {
        if (StringUtils.isBlank(zkClusterKey)) {
            return Response.success(alarmStatisticsService.getAbnormalContainers());
        }
        return Response.success(alarmStatisticsService.getAbnormalContainers(zkClusterKey));
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    //@Audit
    @PostMapping(value = "/setAbnormalJobMonitorStatusToRead")
    public Response setAbnormalJobMonitorStatusToRead(@RequestParam String uuid)
            throws RuntimeException {
        //assertIsPermitted(PermissionKeys.alarmCenterSetAbnormalJobRead);
        alarmStatisticsService.setAbnormalJobMonitorStatusToRead(uuid);
        return new Response();
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    //@Audit
    @PostMapping(value = "/setTimeout4AlarmJobMonitorStatusToRead")
    public Response setTimeout4AlarmJobMonitorStatusToRead(@RequestParam String uuid)
            throws RuntimeException {
        //assertIsPermitted(PermissionKeys.alarmCenterSetTimeout4AlarmJobRead);
        alarmStatisticsService.setTimeout4AlarmJobMonitorStatusToRead(uuid);
        return new Response();
    }

}
